//
//  HAYHForgetPwdVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAYHForgetPwdVC.h"

@interface HAYHForgetPwdVC ()

/***/
@property (nonatomic, strong) UITextField *phoneTextField;
/***/
@property (nonatomic, strong) UITextField *codeTextField;
/***/
@property (nonatomic, strong) UITextField *pwdTextField;
/***/
@property (nonatomic, strong) UITextField *pwdTTextField;
/***/
@property (nonatomic, strong) UIButton *codeButton;


@end

@implementation HAYHForgetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:@"OYCountDownNotification" object:nil];
    [kCountDownManager addSourceWithIdentifier:@"YHForgetPwdDownNotification"];
    
    // Do any additional setup after loading the view.
}

-(void)dealloc {
    [kCountDownManager removeSourceWithIdentifier:@"YHForgetPwdDownNotification"];
}

- (void)countDownNotification {
    NSInteger timeInterval = [kCountDownManager timeIntervalWithIdentifier:@"YHForgetPwdDownNotification"];
    
    if (61-timeInterval > 0) {
        [self.codeButton setTitle:[NSString stringWithFormat:@"%ld",(long)(61-timeInterval)] forState:UIControlStateNormal];
        self.codeButton.enabled = NO;
    }else {
        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeButton.enabled = YES;
    }
}


-(void)codeButtonClick {
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"sms/send";
    request.parms = @{
        @"event":@"resetpwd",
        @"mobile":self.phoneTextField.text?:@"",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:dataModel.msg];
                [kCountDownManager reloadSourceWithIdentifier:@"YHForgetPwdDownNotification"];
                [kCountDownManager start];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

-(void)loginButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)sureButtonClick {
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }
    
    if ([NSString isEmptyString:self.codeTextField.text]) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    
    if ([NSString isEmptyString:self.pwdTextField.text]) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }

    if (![self.pwdTextField.text isEqualToString:self.pwdTTextField.text]) {
        [MBProgressHUD showError:@"2次密码不一致"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"auth/resetpwd";
    request.parms = @{
        @"mobile":self.phoneTextField.text?:@"",
        @"verifi_code":self.codeTextField.text?:@"",
        @"password":self.pwdTextField.text?:@"",
        @"repassword":self.pwdTTextField.text?:@"",
        @"device":@"ios",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"修改成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:20 textColor:@"#2E63B1" text:@"找回密码" alignment:0];
    [self.view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(app_topH+44+40);
    }];
    
    UIView *line1 = [UIView ha_createView];
    line1.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(titLabel.mas_bottom).offset(120);
        make.height.equalTo(@0.8);
    }];
    
    UIView *line2 = [UIView ha_createView];
    line2.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line1.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];
    
    UIView *line3 = [UIView ha_createView];
    line3.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line2.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];

    
    UIView *line4 = [UIView ha_createView];
    line4.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line4];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line3.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];

    UIImageView *phoneImgView = [UIImageView ha_createImageView];
    phoneImgView.image = [UIImage imageNamed:@""];
    [self.view addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line1.mas_top).offset(-12);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];

    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _phoneTextField.font = [UIFont systemFontOfSize:14];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_phoneTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.left.equalTo(phoneImgView.mas_right).offset(5);
        make.right.equalTo(line1);
        make.height.equalTo(@28);
    }];
    
    UIImageView *codeImgView = [UIImageView ha_createImageView];
    codeImgView.image = [UIImage imageNamed:@""];
    [self.view addSubview:codeImgView];
    [codeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line2.mas_top).offset(-12);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];
    
    _codeButton = [[UIButton alloc]init];
    [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_codeButton setTitleColor:[UIColor colorWithHexString:@"#5f67fc"] forState:UIControlStateNormal];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_codeButton addTarget:self action:@selector(codeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeButton];
    [_codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.right.equalTo(line2);
        make.height.equalTo(@30);
        make.width.equalTo(@84);

    }];

    _codeTextField = [[UITextField alloc]init];
    _codeTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _codeTextField.font = [UIFont systemFontOfSize:13];
    _codeTextField.placeholder = @"请输入验证码";
    _codeTextField.secureTextEntry = YES;
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_codeTextField];
    [self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeImgView);
        make.left.equalTo(codeImgView.mas_right).offset(5);
        make.right.equalTo(self.codeButton.mas_left).offset(-5);
        make.height.equalTo(@28);
    }];


    UIImageView *pwdImgView = [UIImageView ha_createImageView];
    pwdImgView.image = [UIImage imageNamed:@""];
    [self.view addSubview:pwdImgView];
    [pwdImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line3.mas_top).offset(-12);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];

    _pwdTextField = [[UITextField alloc]init];
    _pwdTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _pwdTextField.font = [UIFont systemFontOfSize:13];
    _pwdTextField.placeholder = @"请输入登录密码";
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImgView);
        make.left.equalTo(pwdImgView.mas_right).offset(5);
        make.right.equalTo(line3);
        make.height.equalTo(@28);
    }];
    
    UIImageView *pwdTImgView = [UIImageView ha_createImageView];
    pwdTImgView.image = [UIImage imageNamed:@""];
    [self.view addSubview:pwdTImgView];
    [pwdTImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line4.mas_top).offset(-12);
        make.width.mas_offset(20);
        make.height.mas_offset(20);
    }];

    _pwdTTextField = [[UITextField alloc]init];
    _pwdTTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _pwdTTextField.font = [UIFont systemFontOfSize:13];
    _pwdTTextField.placeholder = @"请再次输入登录密码";
    _pwdTTextField.secureTextEntry = YES;
    _pwdTTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_pwdTTextField];
    [self.pwdTTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdTImgView);
        make.left.equalTo(pwdTImgView.mas_right).offset(5);
        make.right.equalTo(line4);
        make.height.equalTo(@28);
    }];

    UIButton *loginButton = [[UIButton alloc]init];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"马上登录" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [loginButton setTitleColor:[UIColor colorWithHexString:@"#676ADD"] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@80);
        make.height.equalTo(@28);
        make.top.equalTo(line4.mas_bottom).offset(18);
        make.right.equalTo(line4);
    }];
    
    if(self.isLogin == NO) {
        loginButton.hidden = YES;
    }

    UIButton *sureButton = [[UIButton alloc]init];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:16];
    sureButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    sureButton.layer.cornerRadius = 4;
    sureButton.layer.masksToBounds = true;
    [sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureButton];
    [sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(line4.mas_bottom).offset(108);
        make.height.mas_offset(44);
    }];
}
@end
